import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

var Util = require("../../utils/utils.js");
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    question: {},
    // 是否展示
    show: false,
    // 积分点
    integral: 1,
    // 时间
    viewTime: 0,
    // 定时器
    timer: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    // this.setData({
    //   review: options.review
    // })

    // 设置积分点
    // 设置浏览时间
    this.setData({
      integral: 1,
      viewTime: 0
    });

    // console.log(this.data.integral);

    // 1.此页面需要登录，检查是否登录
    let user = wx.getStorageSync('user');
    if (user == "" || user == null || user == undefined) {
      // console.log("还没有登录哦！");
      Dialog.confirm({
          title: '权限不足',
          message: '此操作需要登录',
        })
        .then(() => {
          // on confirm
          // 跳转到个人主页登录
          wx.reLaunch({
            url: '/pages/personal/index?login=true'
          })
        })
        .catch(() => {
          // on cancel
          // 啥也不做
        });
    } else {
      // 已登录// 2.拉取文章详情
      let {
        id
      } = options;
      // console.log(id);
      var that = this;
      let url = `${app.globalData.baseURL}/api/question/question/info/${id}`;
      // 查询文章
      wx.request({
        url,
        success(res) {
          let {
            question
          } = res.data;
          // console.log(question);
          // question.updateTime = Util.utc2beijing(question.updateTime);
          if (question != undefined && question != null) {
            question.updateTime = Util.formatTime(new Date(question.updateTime));
          }
          let show = question == undefined || question == null ? false : true;
          // console.log("show = " + show);
          that.setData({
            question,
            show
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //实时更新时间
    this.data.timer = setInterval(res => {
      that.setData({
        viewTime: that.data.viewTime + 1,
      })
      // console.log(this.data.viewTime);
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 关闭定时器
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 关闭定时器
    clearInterval(this.data.timer);
    // if (this.data.review == true) {
    //   return;
    // }
    if (this.data.show) {
      // 获取时间，判定是否增长积分点
      let {
        integral,
        viewTime
      } = this.data;
      let {
        id
      } = this.data.question;

      let user = wx.getStorageSync('user');

      // 积分对象
      let growthEntity = {
        memberId: user.id,
        changeCount: integral,
        note: "阅读文章积分上涨",
        sourceType: 3,
        delFlag: 0,
        createTime: new Date(),
        updateTime: new Date(),
      };

      // 超过 60s 才加积分点
      if (viewTime >= 60) {
        let url = `${app.globalData.baseURL}/api/member/growth/save`;
        wx.request({
          url,
          method: "POST",
          data: growthEntity,
          success(res) {
            // console.log("integral");
            // console.log(res);
          }
        });

        // 用户积分点也需同步增加
        url = `${app.globalData.baseURL}/api/member/member/${user.id}/${integral}`;
        wx.request({
          url,
          success(res) {
            // console.log("member integral");
            // console.log(res);
          }
        });

        // 添加记录到“学习时长表”、“学习记录表”
        let {
          question
        } = this.data;

        // console.log("question.id = " + question.id);
        // console.log("question.type = " + question.type);

        // 学习时长对象
        let smsStudyTime = {
          quesType: question.type,
          memberId: user.id,
          totalTime: Math.floor(viewTime / 60),
          delFlag: 0,
          createTime: new Date(),
          updateTime: new Date()
        };
        url = `${app.globalData.baseURL}/api/study/studytime/save`;
        wx.request({
          url,
          method: "POST",
          data: smsStudyTime,
          success(res) {
            // console.log("studyTime");
            // console.log(res);
          }
        })

        // 学习记录对象
        let smsViewLog = {
          quesId: question.id,
          quesType: question.type,
          memberId: user.id,
          delFlag: 0,
          createTime: new Date(),
          updateTime: new Date()
        };
        url = `${app.globalData.baseURL}/api/study/viewlog/save`;
        wx.request({
          url,
          method: "POST",
          data: smsViewLog,
          success(res) {
            // console.log("viewLog");
            // console.log(res);
          }
        })

      }
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})